<?php
    class mysql{
        private $db;
        private $tag;
        private $connect_list;
        function connect($link,$name,$passwd,$database){
            $this->connect_list=array($link,$name,$passwd,$database);
        }
        
        function real_connect($str=''){
            list($link,$name,$passwd,$database)=$this->connect_list;
            if($this->db){
                return 1;
            }
            if(@!$this->db=mysql_connect($link,$name,$passwd)){
                print_r($str);
                throw new my_exception("DB CAN NOT CONNECTED!",MYSQL_ERROR);
            }
            if(!mysql_select_db($database,$this->db)){
                throw new my_exception("DB ERROR",MYSQL_ERROR);
            }
            mysql_query("set names utf8",$this->db);
        }
        
        function begin(){
            mysql_query("BEGIN");
            $this->tag=1;
        }
        
        function commit(){
            $this->tag=0;
            return mysql_query("COMMIT");
        }
        
        function rollback(){
            mysql_query("ROLLBACK");
            $this->tag=0;
        }
        
        function __destruct(){
            if($this->tag==1){
                $this->rollback();
            }
        }
        
        function query($query){
            if(defined("PAGE_DB_CACHE_TIME") && !$this->tag && !stristr(trim($query),"select")){
                return ;
            }
            $query=str_replace("`jiaochenhui`.", "", $query);
            $this->real_connect($query);
            $st=microtime(true);
            if($result=mysql_query($query,$this->db)){
                $et=microtime(true);
                $sub_t=$et-$st;
                if($sub_t>0.1){
                    log::write("sql_exc_".date("Y-m-d"), "TIME:".date("Y-m-d H:i:s")."\r\n");
                    log::write("sql_exc_".date("Y-m-d"), "EXC_TIME:".$sub_t."\r\n");
                    log::write("sql_exc_".date("Y-m-d"), "trace:".var_export(debug_backtrace(),1)."\r\n");
                    log::write("sql_exc_".date("Y-m-d"), "SQL:".$query."\r\n");
                    
                }
                return $result;
            }
            else{
                throw new Exception(mysql_error().":".$query,1);
            }
        }
        
        function get_one($query,$key=0){
            
            $query_str=$query;
            if(defined("PAGE_DB_CACHE_TIME") && !$this->tag){
                $cache=cache::get_cache_handle("memcached");
                $result=$cache->get_data($query_str);
                if($result=="cache_mysql_empty_result"){
                    $result='';
                }
                if($result===false){
                    $query=$this->query($query);
                    $result=mysql_fetch_array($query);
                    if(!$result){
                        $result="cache_mysql_empty_result";
                    }
                    $cache->set_data($query_str,$result,PAGE_DB_CACHE_TIME);
                }
            }
            else{
                $query=$this->query($query);
                $result=mysql_fetch_array($query);
            }
           
            if($key && !empty($result)){
                if(isset($result[$key])){
                    return $result[$key];
                }
                else{
                    return 0;
//                     throw new my_exception("你所查询的元素".$key."不存在",MYSQL_ERROR);
                }
            }
            else{
               
                return $result;
            }
        }
        
        function get_array($query,$one_step=0,$one_step_key="",$key_as_key=""){
            $query_str=$query;
            if(defined("PAGE_DB_CACHE_TIME") && !$this->tag){
                $cache=cache::get_cache_handle("memcached");
                $row=$cache->get_data($query_str);
                if($row=="cache_mysql_empty_result"){
                    $row='';
                }
                if($row===false){
                    $query=$this->query($query);
                    $row=array();
                    while($result=mysql_fetch_array($query,MYSQL_ASSOC)){
                        if($one_step && $one_step_key){
                            $row[]=$result[$one_step_key];
                        }
                        elseif($key_as_key){
                            $row[$result[$key_as_key]]=$result;
                        }
                        else{
                            $row[]=$result;
                        }
                    }
                    if(empty($row)){
                        $row_cache="cache_mysql_empty_result";
                    }
                    else{
                        $row_cache=$row;
                    }
                    $cache->set_data($query_str,$row_cache,PAGE_DB_CACHE_TIME);
                }
            }
            else{
                $query=$this->query($query);
                $row=array();
                while($result=mysql_fetch_array($query,MYSQL_ASSOC)){
                    if($one_step && $one_step_key){
                        $row[]=$result[$one_step_key];
                    }
                    elseif($key_as_key){
                        $row[$result[$key_as_key]]=$result;
                    }
                    else{
                        $row[]=$result;
                    }
                }
            }
            return $row;
        }
        function insert_id(){
            return mysql_insert_id($this->db);
        }
        
        
    }